<?php
class License extends AppModel {
	var $name = 'License';
	var $validate = array(
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'client_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Client' => array(
			'className' => 'Client',
			'foreignKey' => 'client_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Day' => array(
			'className' => 'Day',
			'foreignKey' => 'day_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
        
        /**
         * 
         * @param type $client_id
         * @param type $profesional_id
         * @param type $consulta_desde
         * @param type $consulta_hasta
         * @return type
         */
        function getLicenses($client_id, $profesional_id, $consulta_desde, $consulta_hasta){
            
            $sql = "SELECT * 
                    FROM licenses as License
                    WHERE ( fecha_desde BETWEEN '$consulta_desde' AND '$consulta_hasta') 
                        OR (fecha_hasta BETWEEN '$consulta_desde' AND '$consulta_hasta')";
            $licencias = $this->query($sql);
            
            $arrLicenses = array();
            if($licencias){
                
                foreach($licencias as $l){
                    
                    $desde = $l['License']['fecha_desde'];
                    $hasta = $l['License']['fecha_hasta'];
                    $startTime = strtotime( $desde.' 12:00' );
                    $endTime = strtotime( $hasta.' 12:00' );

                    // Loop between timestamps, 24 hours at a time
                    for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) {
                      $thisDate = date( 'Y-m-d', $i ); // 2010-05-01, 2010-05-02, etc
                      $x = array();
                      $x['id'] = $l['License']['id'];
                      $x['name'] = $l['License']['name'];
                      $x['fecha'] = $thisDate;
                      $x['hora_desde'] = $l['License']['hora_desde'];
                      $x['hora_hasta'] = $l['License']['hora_hasta'];
                      $arrLicenses[$thisDate] = $x;
                    }                    

                }
                
            }
            return $arrLicenses;
            
        }
}
?>